﻿using CodeGenerator.Common;
using CodeGenerator.Core;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;

namespace CodeGenerator.Core
{
    public class ToChm
    {
        /// <summary>
        /// 生成数据库表信息
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="KeepNull"></param>
        /// <param name="Path"></param>
        /// <param name="hasReturn"></param>
        /// <param name="tableDesc"></param>
        public static void CreateHtml1(DataTable dt, bool KeepNull, string Path, bool hasReturn, string tableDesc)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">");
            stringBuilder.AppendLine("<html xmlns=\"http://www.w3.org/1999/xhtml\">");
            stringBuilder.AppendLine("<head>");
            stringBuilder.AppendLine("<title>数据库表目录</title>");
            stringBuilder.AppendLine("    <style type=\"text/css\">");
            stringBuilder.AppendLine("        body");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            font-size: 9pt;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("        .styledb");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            font-size: 14px;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("        .styletab");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            font-size: 14px;");
            stringBuilder.AppendLine("            padding-top: 15px;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("        a");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            color: #015FB6;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("        a:link, a:visited, a:active");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            color: #015FB6;");
            stringBuilder.AppendLine("            text-decoration: none;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("        a:hover");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            color: #E33E06;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("    </style>");
            stringBuilder.AppendLine("</head>");
            stringBuilder.AppendLine("<body>");
            stringBuilder.AppendLine("    <div style=\"text-align: center\">");
            stringBuilder.AppendLine("        <div>");
            stringBuilder.AppendLine("            <table border=\"0\" cellpadding=\"5\" cellspacing=\"0\" width=\"90%\">");
            stringBuilder.AppendLine("                <tr>");
            stringBuilder.AppendLine("                    <td bgcolor=\"#FBFBFB\">");
            stringBuilder.AppendLine("                        <table cellspacing=\"0\" cellpadding=\"5\" border=\"1\" width=\"100%\" bordercolorlight=\"#D7D7E5\" bordercolordark=\"#D3D8E0\">");
            stringBuilder.AppendLine("                        <caption>");
            stringBuilder.AppendLine(FormatString("        <div class=\"styletab\">{0}{1}{2}</div>", new object[]
			{
				dt.TableName, 
				(tableDesc.Length == 0) ? string.Empty : ("  （" + tableDesc + "） "), 
				hasReturn ? "<a href='../数据库表目录.html' style='float: right; margin-top: 6px;'>返回目录</a>" : string.Empty
			}));
            stringBuilder.AppendLine("                        </caption>");
            stringBuilder.AppendLine("                        <tr bgcolor=\"#F0F0F0\">");
            IEnumerator enumerator = dt.Columns.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    DataColumn dataColumn = (DataColumn)enumerator.Current;
                    stringBuilder.AppendLine(FormatString("            <td>{0}</td>", new object[]
					{
						dataColumn.ColumnName
					}));
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            stringBuilder.AppendLine("                         </tr>");
            enumerator = dt.Rows.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    DataRow dataRow = (DataRow)enumerator.Current;
                    stringBuilder.AppendLine("            <tr>");
                    IEnumerator enumerator2 = dt.Columns.GetEnumerator();
                    try
                    {
                        while (enumerator2.MoveNext())
                        {
                            DataColumn dataColumn2 = (DataColumn)enumerator2.Current;
                            if (KeepNull && dataRow[dataColumn2.ColumnName] == DBNull.Value)
                            {
                                stringBuilder.AppendLine("            <td>&nbsp;</td>");
                            }
                            else
                            {
                                stringBuilder.AppendLine(FormatString("            <td>{0}</td>", new object[]
								{
									(dataRow[dataColumn2.ColumnName].ToString().Trim().Length > 0) ? dataRow[dataColumn2.ColumnName].ToString() : "&nbsp;"
								}));
                            }
                        }
                    }
                    finally
                    {
                        IDisposable disposable2 = enumerator2 as IDisposable;
                        if (disposable2 != null)
                        {
                            disposable2.Dispose();
                        }
                    }
                    stringBuilder.AppendLine("            </tr>");
                }
            }
            finally
            {
                IDisposable disposable3 = enumerator as IDisposable;
                if (disposable3 != null)
                {
                    disposable3.Dispose();
                }
            }
            stringBuilder.AppendLine("                        </table>");
            stringBuilder.AppendLine("                    </td>");
            stringBuilder.AppendLine("                </tr>");
            stringBuilder.AppendLine("            </table>");
            stringBuilder.AppendLine("        </div>");
            stringBuilder.AppendLine("    </div>");
            stringBuilder.AppendLine("</body>");
            stringBuilder.AppendLine("</html>");
            File.WriteAllText(Path, stringBuilder.ToString(), Encoding.GetEncoding("gb2312"));
        }

        /// <summary>
        /// 生成表数据信息
        /// </summary>
        /// <param name="dt"></param>
        /// <param name="KeepNull"></param>
        /// <param name="Path"></param>
        public static void CreateHtml2(DataTable dt, bool KeepNull, string Path)
        {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.AppendLine("<html>");
            stringBuilder.AppendLine("<head>");
            stringBuilder.AppendLine(FormatString("    <title>J{0}</title>", new object[]
			{
				dt.TableName
			}));
            stringBuilder.AppendLine("    <meta http-equiv=\"content-type\" content=\"text/html; charset=GBK\">");
            stringBuilder.AppendLine("    <style type=\"text/css\">");
            stringBuilder.AppendLine("        table");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            background-color: #F2F2F5;");
            stringBuilder.AppendLine("            border-width: 1px 1px 0px 1px;");
            stringBuilder.AppendLine("            border-color: #C9CBD3;");
            stringBuilder.AppendLine("            border-style: solid;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("        td");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            color: #000000;");
            stringBuilder.AppendLine("            font-family: Tahoma,Arial,Helvetica,Geneva,sans-serif;");
            stringBuilder.AppendLine("            font-size: 9pt;");
            stringBuilder.AppendLine("            background-color: #EAEFF5;");
            stringBuilder.AppendLine("            padding: 8px;");
            stringBuilder.AppendLine("            background-color: #F2F2F5;");
            stringBuilder.AppendLine("            border-color: #ffffff #ffffff #cccccc #ffffff;");
            stringBuilder.AppendLine("            border-style: solid solid solid solid;");
            stringBuilder.AppendLine("            border-width: 1px 0px 1px 0px;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("        th");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            font-family: Tahoma,Arial,Helvetica,Geneva,sans-serif;");
            stringBuilder.AppendLine("            font-size: 9pt;");
            stringBuilder.AppendLine("            padding: 8px;");
            stringBuilder.AppendLine("            background-color: #CFE0F1;");
            stringBuilder.AppendLine("            border-color: #ffffff #ffffff #cccccc #ffffff;");
            stringBuilder.AppendLine("            border-style: solid solid solid none;");
            stringBuilder.AppendLine("            border-width: 1px 0px 1px 0px;");
            stringBuilder.AppendLine("            white-space: nowrap;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("        a:link, a:visited, a:active");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            color: #015FB6;");
            stringBuilder.AppendLine("            text-decoration: none;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("        a:hover");
            stringBuilder.AppendLine("        {");
            stringBuilder.AppendLine("            color: #E33E06;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine("    </style>");
            stringBuilder.AppendLine("    <script type=\"text/javascript\">");
            stringBuilder.AppendLine("        window.apex_search = {};");
            stringBuilder.AppendLine("        apex_search.init = function () {");
            stringBuilder.AppendLine("            this.rows = document.getElementById('data').getElementsByTagName('TR');");
            stringBuilder.AppendLine("            this.rows_length = apex_search.rows.length;");
            stringBuilder.AppendLine("            this.rows_text = [];");
            stringBuilder.AppendLine("            for (var i = 0; i < apex_search.rows_length; i++) {");
            stringBuilder.AppendLine("                this.rows_text[i] = (apex_search.rows[i].innerText) ? apex_search.rows[i].innerText.toUpperCase() : apex_search.rows[i].textContent.toUpperCase();");
            stringBuilder.AppendLine("            }");
            stringBuilder.AppendLine("            this.time = false;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("        apex_search.lsearch = function () {");
            stringBuilder.AppendLine("            this.term = document.getElementById('S').value.toUpperCase();");
            stringBuilder.AppendLine("            for (var i = 0, row; row = this.rows[i], row_text = this.rows_text[i]; i++) {");
            stringBuilder.AppendLine("                row.style.display = ((row_text.indexOf(this.term) != -1) || this.term === '') ? '' : 'none';");
            stringBuilder.AppendLine("            }");
            stringBuilder.AppendLine("            this.time = false;");
            stringBuilder.AppendLine("        }");
            stringBuilder.AppendLine();
            stringBuilder.AppendLine("        apex_search.search = function (e) {");
            stringBuilder.AppendLine("            var keycode;");
            stringBuilder.AppendLine("            if (window.event) { keycode = window.event.keyCode; }");
            stringBuilder.AppendLine("            else if (e) { keycode = e.which; }");
            stringBuilder.AppendLine("            else { return false; }");
            stringBuilder.AppendLine("            if (keycode == 13) {");
            stringBuilder.AppendLine("                apex_search.lsearch();");
            stringBuilder.AppendLine("            }");
            stringBuilder.AppendLine("            else { return false; }");
            stringBuilder.AppendLine("        }</script>");
            stringBuilder.AppendLine("</head>");
            stringBuilder.AppendLine("<body onload=\"apex_search.init();\">");
            stringBuilder.AppendLine("    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
            stringBuilder.AppendLine("        <tbody>");
            stringBuilder.AppendLine("            <tr>    ");
            stringBuilder.AppendLine("                <td>");
            stringBuilder.AppendLine("                    <input type=\"text\" size=\"30\" maxlength=\"1000\" value=\"\" id=\"S\" onkeyup=\"apex_search.search(event);\" /><input type=\"button\" value=\"Search\" onclick=\"apex_search.lsearch();\" />");
            stringBuilder.AppendLine("                </td>");
            stringBuilder.AppendLine("                <td>");
            stringBuilder.AppendLine("                        " + dt.TableName);
            stringBuilder.AppendLine("                </td>");
            stringBuilder.AppendLine("            </tr>");
            stringBuilder.AppendLine("        </tbody>");
            stringBuilder.AppendLine("    </table>");
            stringBuilder.AppendLine("    <br />");
            stringBuilder.AppendLine("    <table border=\"0\" cellpadding=\"0\" cellspacing=\"0\">");
            stringBuilder.AppendLine("        <tr>");
            IEnumerator enumerator = dt.Columns.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    DataColumn dataColumn = (DataColumn)enumerator.Current;
                    stringBuilder.AppendLine(FormatString("            <th>{0}</th>", new object[]
					{
						dataColumn.ColumnName
					}));
                }
            }
            finally
            {
                IDisposable disposable = enumerator as IDisposable;
                if (disposable != null)
                {
                    disposable.Dispose();
                }
            }
            stringBuilder.AppendLine("        </tr>");
            stringBuilder.AppendLine("        <tbody id=\"data\">");
            enumerator = dt.Rows.GetEnumerator();
            try
            {
                while (enumerator.MoveNext())
                {
                    DataRow dataRow = (DataRow)enumerator.Current;
                    stringBuilder.AppendLine("            <tr>");
                    IEnumerator enumerator2 = dt.Columns.GetEnumerator();
                    try
                    {
                        while (enumerator2.MoveNext())
                        {
                            DataColumn dataColumn2 = (DataColumn)enumerator2.Current;
                            if (KeepNull && dataRow[dataColumn2.ColumnName] == DBNull.Value)
                            {
                                stringBuilder.AppendLine("            <td>&nbsp;</td>");
                            }
                            else
                            {
                                stringBuilder.AppendLine(FormatString("            <td>{0}</td>", new object[]
								{
									(dataRow[dataColumn2.ColumnName].ToString().Length > 0) ? dataRow[dataColumn2.ColumnName].ToString() : "&nbsp;"
								}));
                            }
                        }
                    }
                    finally
                    {
                        IDisposable disposable2 = enumerator2 as IDisposable;
                        if (disposable2 != null)
                        {
                            disposable2.Dispose();
                        }
                    }
                    stringBuilder.AppendLine("            </tr>");
                }
            }
            finally
            {
                IDisposable disposable3 = enumerator as IDisposable;
                if (disposable3 != null)
                {
                    disposable3.Dispose();
                }
            }
            stringBuilder.AppendLine("        </tbody>");
            stringBuilder.AppendLine("    </table>");
            stringBuilder.AppendLine("</body>");
            stringBuilder.AppendLine("</html>");
            File.WriteAllText(Path, stringBuilder.ToString(), Encoding.GetEncoding("gb2312"));
        }

        public static string FormatString(string s, params object[] args)
        {
            return string.Format(s, args);
        }

    }
}
